<?php

namespace Yolo\IdGenerator;

use Exception;
use Yolo\Config\Annotations\UseConfig;
use Yolo\Di\Annotations\Singleton;

/**
 * ID Generator Factory
 * @method string generateId() Generate a new ID
 */
#[Singleton]
class IdGeneratorFactory
{
    private readonly IdGeneratorInterface $idGenerator;

    /**
     * ID Generator Factory
     * @param array $config
     * @throws Exception
     */
    public function __construct(
        #[UseConfig('uuid', [
            'type' => 'required|string',
            'snowflake.epoch' => 'sometimes|required|date:Y-m-d H:i:s',
            'snowflake.datacenterId' => 'sometimes|required|integer|min:0',
            'snowflake.workerId' => 'sometimes|required|integer|min:0'
        ])]
        private readonly array $config
    ){
        $this->idGenerator = match ($this->config['type']) {
            'uuid' => new Uuid(),
            default => new Snowflake($this->config['snowflake']),
        };
    }

    public function __call(string $name, array $arguments)
    {
        return $this->idGenerator->$name(...$arguments);
    }
}
